//1356. 根据数字二进制下 1 的数目排序
//思路：修改sort的规则，自己写一个判断二进制序列1多少比较的接口

#include <algorithm>
#include <vector>
using namespace std;

class Solution {
private:
    static int bitCount(int n)
    {
        int count = 0;
        while(n)
        {
            if(n & 1) count++;
            n >>= 1;
        }
        return count;
    }
    static bool cmp(int a, int b)
    {
        int bit_a = bitCount(a);
        int bit_b = bitCount(b);
        if(bit_a == bit_b) return a < b;
        return bit_a < bit_b;
    }
public:    
    vector<int> sortByBits(vector<int>& arr) 
    {
        sort(arr.begin(), arr.end(), cmp);
        return arr;
    }
};